package com.sgb.kjwl.view.ui.adapter.shopping;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemGroupBuyListBinding;
import com.sgb.jicai.entity.CentralizedMiningEntity;
import com.sgb.kjwl.view.ui.activity.manage.ShopInfoForCompanyTradesActivity;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter3;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;

public class GroupBuyAdapter extends BaseRecycleAdapter3<CentralizedMiningEntity> {
    private Context context;
    private ItemClickCallBack itemCallBack;
    public GroupBuyAdapter(Context context, List<CentralizedMiningEntity> datas) {
        super(context, R.layout.item_group_buy_list, datas);
        this.context = context;
    }

    @Override
    public void convert(ItemViewHolder holder, final CentralizedMiningEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        ((ItemGroupBuyListBinding) holder.getmBinding()).ent.setOnClickListener(view ->
        ShopInfoForCompanyTradesActivity.startAct(context, entity.getSupplierId(), "", 0, 0));
        updateRecycler(((ItemGroupBuyListBinding) holder.getmBinding()).childRecycler, entity);
        updateImageStatus(((ItemGroupBuyListBinding) holder.getmBinding()).imgTab, entity);
        updateGroupBuyType(((ItemGroupBuyListBinding) holder.getmBinding()).tvStatus, entity);

        ((ItemGroupBuyListBinding) holder.getmBinding()).rlContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemCallBack != null) {
                    itemCallBack.onItemClick(entity);//可用此替换setOnItemClick
                }
            }
        });
    }

    private void updateRecycler(RecyclerView recyclerView, CentralizedMiningEntity entity) {
        recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
        GroupBuyChildAdapter childAdapter = new GroupBuyChildAdapter(recyclerView.getContext(), entity.getGoodsEntity());
        recyclerView.setAdapter(childAdapter);
        childAdapter.setOnItemClick(new AdapterOnItemClick<CentralizedMiningEntity.GoodsEntity>() {
            @Override
            public void onItemClick(CentralizedMiningEntity.GoodsEntity goodsEntity, int position) {//给内层添加点击事件，解决嵌套时点击内层无效果的问题
                if (itemCallBack != null) {
                    itemCallBack.onItemClick(entity);
                }
            }
        });
    }

    private void updateImageStatus(ImageView imageView, CentralizedMiningEntity entity) {
        // 集采状态（1 集采中 2 集采结束 4 集采失败 5 加盟中 6 报价中 ）
        switch (entity.getStatus()) {
            case 2:
                imageView.setImageResource(R.drawable.icon_jcwc);
                break;
            case 4:
                imageView.setImageResource(R.drawable.collect_fail);
                break;
            case 5:
                imageView.setImageResource(R.drawable.icon_jmz);
                break;
            case 6:
                imageView.setImageResource(R.drawable.icon_bjz);
                break;
            default:
                imageView.setImageResource(R.drawable.icon_jcz);
                break;
        }
    }

    private void updateGroupBuyType(TextView textView, CentralizedMiningEntity entity) {
        switch (entity.getGroupBuyType()) {//集采类型: 1即报集采 2 汇总集采  3.企业专项
            case 1:
                textView.setText(textView.getResources().getText(R.string.jbjc));
                textView.setBackgroundResource(R.drawable.shape_blue_button);
                break;
            case 2:
                textView.setText(textView.getResources().getText(R.string.hzcg));
                textView.setBackgroundResource(R.drawable.shape_yellow_button);
                break;
            case 3:

                if ("1".equals(entity.getOfferType())) {
                    textView.setBackgroundResource(R.drawable.hybg);
                    textView.setText("客户报价");
                } else {
                    textView.setBackgroundResource(R.drawable.shape_bule_green_button);
                    textView.setText("市场报价");
                }
                break;
            default:
                break;
        }
    }

    public void setItemClickCallBack(ItemClickCallBack itemCallBack) {
        this.itemCallBack = itemCallBack;
    }

    public interface ItemClickCallBack {
        void onItemClick(CentralizedMiningEntity entity);
    }

}
